package com.controller;

import java.io.PrintWriter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.domain.Jmtj;
import com.domain.JmtjJlx;
import com.service.JmtjService;
import com.utils.ObjectUtils;
import com.utils.Ret;

@Controller
public class JmtjController {
	
	@Autowired
	JmtjService jmtjService;

	@RequestMapping("/jmtj.do")
	String toFamily(@RequestParam(value="jkdah", defaultValue="")String hzxm,
			@RequestParam(value="tjbh", defaultValue="")String tjbh,
			@RequestParam(value="tjrq", defaultValue="")String tjrq,
			@RequestParam(value="pageNum", defaultValue="1")int pageNum,
			@RequestParam(value="pageSize",defaultValue="10")int pageSize, 
			HttpServletRequest request, Model model){
		
		
		String ocode = (String)request.getSession().getAttribute("ocode");
		if(ocode.equals("0")){
			ocode="";
		}
		List<Map<String,Object>> jmtj= new ArrayList<Map<String,Object>>();
		int count = 0;
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("jkdah", hzxm);
		params.put("tjbh", tjbh);
		params.put("tjrq", tjrq);
		params.put("ocode", ocode);
		
		params.put("pageBegin", (pageNum-1)*pageSize);
		params.put("pageSize", pageSize);
		
		
		jmtj = jmtjService.getJmtj(params);
		count = jmtjService.getCount(params);
		params.put("totalCount", count);
		
		
		model.addAttribute("params",params);
		model.addAttribute("jmtj", jmtj);
		return "jmtj/jmtj";
		
	}
	
	@RequestMapping("/toUpdateJmtj")
	public String toUpdateJmtj(@RequestParam("tjbh") int tjbh,Model model){
		 
		String jlxmc = "";//酒类型名称
		try {
			model.addAttribute("jmtj", jmtjService.getJmtjByTjbh(tjbh));
			
			List<Map<String,Object>> listJmtjJlx = jmtjService.getJmtjJlxByTjbh(tjbh);
			for(Map<String,Object> map:listJmtjJlx){
				map.get("mc");
				jlxmc += map.get("mc")+",";
			}
			model.addAttribute("jmtjJlx", jlxmc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "jmtj/toupdatejmtj";
	}
	
	@RequestMapping("/doUpdateJmtj.do")
	public void doUpdateJmtj(HttpServletRequest request, PrintWriter out){
		Ret ret = new Ret();
		ret.setStatusCode("200");
		ret.setMessage("更新成功！");
		Jmtj jmtj = new Jmtj();
		
		int tjbh = ObjectUtils.getIntVal(request.getParameter("tjbh"));
		
		jmtj.setDlpl(ObjectUtils.getIntVal(request.getParameter("dlpl")));
		jmtj.setHxpl(ObjectUtils.getIntVal(request.getParameter("hxpl")));
		jmtj.setJcdlsj(ObjectUtils.getFloatVal(request.getParameter("jcdlsj")));
		jmtj.setJkdah(ObjectUtils.getIntVal(request.getParameter("jkdah")));
		jmtj.setKsxynl(ObjectUtils.getIntVal(request.getParameter("ksxynl")));
		jmtj.setKsyjnl(ObjectUtils.getIntVal(request.getParameter("ksyjnl")));
		jmtj.setMb(ObjectUtils.getIntVal(request.getParameter("mb")));
		jmtj.setMcdlsj(ObjectUtils.getFloatVal(request.getParameter("mb")));
		jmtj.setSg(ObjectUtils.getFloatVal(request.getParameter("sg")));
		jmtj.setTjbh(ObjectUtils.getIntVal(request.getParameter("tjbh")));
		try {
			jmtj.setTjrq(ObjectUtils.getDateVal(request.getParameter("tjrq")));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		jmtj.setTw(ObjectUtils.getFloatVal(request.getParameter("tw")));
		jmtj.setTz(ObjectUtils.getFloatVal(request.getParameter("tz")));
		jmtj.setXw(ObjectUtils.getFloatVal(request.getParameter("xw")));
		jmtj.setXyl(ObjectUtils.getIntVal(request.getParameter("xyl")));
		jmtj.setXyzt(ObjectUtils.getIntVal(request.getParameter("xyzt")));
		jmtj.setYjpl(ObjectUtils.getIntVal(request.getParameter("yjpl")));
		jmtj.setYxy(ObjectUtils.getFloatVal(request.getParameter("yxy")));
		jmtj.setZxy(ObjectUtils.getFloatVal(request.getParameter("zxy")));
		try {
			jmtjService.updateJmtj(jmtj);
			String[] jlx = request.getParameterValues("yjzl");
			jmtjService.deleteJmtjJlxByTjbh(tjbh);
			for(int i=0;i<jlx.length;i++){
				JmtjJlx jmtjjlx = new JmtjJlx();
				jmtjjlx.setJid(ObjectUtils.getIntVal(jlx[i]));
				jmtjjlx.setTjbh(ObjectUtils.getIntVal(request.getParameter("tjbh")));
				jmtjService.addJmtjJlx(jmtjjlx);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.setMessage("更新失败！");
			ret.setStatusCode("500");
		}
		
		
		out.print(ret.toJsonString());
		out.flush();
		out.close();
		
	}
	
	
	@RequestMapping("/toAddJmtj.do")
	public String toAddJmtj(Model model,PrintWriter out){
		int tjbh = 0;
		try {
			 tjbh = jmtjService.getTjbh();
			 model.addAttribute("tjbh", tjbh);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "jmtj/toaddjmtj";
	}
	
	@RequestMapping("/deleteJmtj.do")
	public void deleteJmtj(@RequestParam("tjbh") int tjbh,PrintWriter out){
		Ret ret = new Ret();
		ret.setStatusCode("200");
		ret.setMessage("删除成功！");
		try {
			jmtjService.deleteJmtj(tjbh);
		} catch (Exception e) {
			e.printStackTrace();
			ret.setStatusCode("500");
			ret.setMessage("删除失败！");
		}
		
		out.print(ret.toJsonString());
		out.flush();
		out.close();
	}
	
	@RequestMapping("doAddJmtj.do")
	public void addJmtj(HttpServletRequest request, PrintWriter out){
		Ret ret = new Ret();
		ret.setStatusCode("200");
		ret.setMessage("更新成功！");
		Jmtj jmtj = new Jmtj();
		
		int tjbh = ObjectUtils.getIntVal(request.getParameter("tjbh"));
		
		jmtj.setDlpl(ObjectUtils.getIntVal(request.getParameter("dlpl")));
		jmtj.setHxpl(ObjectUtils.getIntVal(request.getParameter("hxpl")));
		jmtj.setJcdlsj(ObjectUtils.getFloatVal(request.getParameter("jcdlsj")));
		jmtj.setJkdah(ObjectUtils.getIntVal(request.getParameter("jkdah")));
		jmtj.setKsxynl(ObjectUtils.getIntVal(request.getParameter("ksxynl")));
		jmtj.setKsyjnl(ObjectUtils.getIntVal(request.getParameter("ksyjnl")));
		jmtj.setMb(ObjectUtils.getIntVal(request.getParameter("mb")));
		jmtj.setMcdlsj(ObjectUtils.getFloatVal(request.getParameter("mb")));
		jmtj.setSg(ObjectUtils.getFloatVal(request.getParameter("sg")));
		jmtj.setTjbh(ObjectUtils.getIntVal(request.getParameter("tjbh")));
		try {
			jmtj.setTjrq(ObjectUtils.getDateVal(request.getParameter("tjrq")));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		jmtj.setTw(ObjectUtils.getFloatVal(request.getParameter("tw")));
		jmtj.setTz(ObjectUtils.getFloatVal(request.getParameter("tz")));
		jmtj.setXw(ObjectUtils.getFloatVal(request.getParameter("xw")));
		jmtj.setXyl(ObjectUtils.getIntVal(request.getParameter("xyl")));
		jmtj.setXyzt(ObjectUtils.getIntVal(request.getParameter("xyzt")));
		jmtj.setYjpl(ObjectUtils.getIntVal(request.getParameter("yjpl")));
		jmtj.setYxy(ObjectUtils.getFloatVal(request.getParameter("yxy")));
		jmtj.setZxy(ObjectUtils.getFloatVal(request.getParameter("zxy")));
		try {
			jmtjService.addJmtj(jmtj);
			String[] jlx = request.getParameterValues("yjzl");
			for(int i=0;i<jlx.length;i++){
				JmtjJlx jmtjjlx = new JmtjJlx();
				jmtjjlx.setJid(ObjectUtils.getIntVal(jlx[i]));
				jmtjjlx.setTjbh(ObjectUtils.getIntVal(request.getParameter("tjbh")));
				jmtjService.addJmtjJlx(jmtjjlx);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.setMessage("更新失败！");
			ret.setStatusCode("500");
		}
		
		
		out.print(ret.toJsonString());
		out.flush();
		out.close();
		
	}
}
